﻿using ComicBook.Data.IDataAdapters.Interfaces;
using ComicBook.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ComicBook.Data.IDataAdapters.Adapters
{
    public class ComicBookAdapter:IComicBookAdapter
    {
        /***************Comicbook CRUD*********************/
        public DataModels.Comic AddComic(DataModels.Comic comic)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                comic = db.Comics.Add(comic);
                db.SaveChanges();
            }
            return comic;
        }

        public List<DataModels.Comic> GetAllComics()
        {
            List<Comic> comics;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                comics = db.Comics.Include("Characters").OrderBy(x => x.Title).ToList();
            }
            return comics;
        }

        public DataModels.Comic GetComic(int id)
        {
            Comic comic;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                comic = db.Comics.Include("Characters").Where(x => x.Id == id).FirstOrDefault();
            }
            return comic;
        }

        public DataModels.Comic UpdateComic(DataModels.Comic comic)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Comic updatingComic = db.Comics.Find(comic.Id);
                updatingComic.setProps(comic);
                db.SaveChanges();
            }
            return comic;
        }

        public void DeleteComic(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Comic comic = db.Comics.Find(id);
                db.Comics.Remove(comic);
                db.SaveChanges();
            }
        }
        public Author GetAuthorForComic(int comicId)
        {
            Author author = new Author();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                AuthorComicBook target = db.AuthorComic.Where(x => x.ComicId == comicId).FirstOrDefault();
                author = db.Authors.Where(x => x.Id == target.AuthorId).FirstOrDefault();
            }
            return author;
        }
        /***************Comicbook CRUD*********************/
        /******************Author CRUD*********************/
        public DataModels.Author AddAuthor(DataModels.Author author)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                author = db.Authors.Add(author);
                db.SaveChanges();
            }
            return author;
        }

        public List<DataModels.Author> GetAllAuthors()
        {
            List<Author> authors;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                authors = db.Authors.OrderBy(x => x.Name).ToList();
            }
            return authors;
        }

        public DataModels.Author GetAuthor(int id)
        {
            Author author;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                author = db.Authors.Where(x => x.Id == id).FirstOrDefault();
            }
            return author;
        }

        public DataModels.Author UpdateAuthor(DataModels.Author author)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Author updatingAuthor = db.Authors.Find(author.Id);
                updatingAuthor.setProps(author);
                db.SaveChanges();
            }
            return author;
        }

        public void DeleteAuthor(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Author author = db.Authors.Find(id);
                db.Authors.Remove(author);
                db.SaveChanges();
            }
        }

        public List<Comic> GetComicsForAuthor(int authorid)
        {
            List<Comic> comicList = new List<Comic>();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                List<AuthorComicBook> ACB = db.AuthorComic.Where(x => x.AuthorId == authorid).ToList();
                foreach (var i in ACB)
                {
                    comicList.Add(db.Comics.Where(x => x.Id == i.ComicId).FirstOrDefault());
                }
            }
            return comicList;
        }
        /******************Author CRUD*********************/
        /***************Character CRUD*********************/
        public DataModels.Character AddCharacter(DataModels.Character character)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                character = db.Characters.Add(character);
                db.SaveChanges();
            }
            return character;
        }

        public List<DataModels.Character> GetAllCharacters()
        {
            List<Character> characters;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                characters = db.Characters.OrderBy(x => x.Name).ToList();
            }
            return characters;
        }

        public DataModels.Character GetCharacter(int id)
        {
            Character character;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                character = db.Characters.Where(x => x.Id == id).FirstOrDefault();
            }
            return character;
        }

        public DataModels.Character UpdateCharacter(DataModels.Character character)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Character updatingChar = db.Characters.Find(character.Id);
                updatingChar.setProps(character);
                db.SaveChanges();
            }
            return character;
        }

        public void DeleteCharacter(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Character character = db.Characters.Find(id);
                db.Characters.Remove(character);
                db.SaveChanges();
            }
        }
        /***************Character CRUD*******************/
        public AuthorComicBook AddAuthorComicBook(AuthorComicBook a)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                a = db.AuthorComic.Add(a);
                db.SaveChanges();
                
            }
            return a;
        }

    }
}
